using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace CppCliWrapper
{
    public class NativeExporterHeaderGenerator : TypeGenerator
    {
        protected override void OnGenerate(Type type, StringBuilder outputText)
        {
            //Here we need an .h and a .cpp because our client
            //will be pure unmanaged code and we can't have our
            //.h #including managed code such as what's generated
            //by the ILBridgeGenerator.

            outputText.AppendLine("#pragma once");
            outputText.AppendLine("#pragma unmanaged");

            //TODO: #define THISDLL_EXPORTS in the project definitions
            Utils.GenerateStandardDllExportDefinitions(outputText);

            string ilBridgeName = Utils.GetILBridgeTypeNameFor(type);
            string nativeExportedName = Utils.GetNativeExportedClassNameFor(type);

            outputText.AppendLine("class " + ilBridgeName + ";");//Fwd decl

            outputText.AppendLine("class THISDLL_API " + nativeExportedName + " {");

            GenerateNativeAggregatedMember(type, outputText);
            outputText.AppendLine("public:");
            outputText.AppendLine(nativeExportedName + "();");  //Constructor
            outputText.AppendLine("~" + nativeExportedName + "();");    //Destructor
            foreach (MethodInfo method in type.GetMethods())
            {
                if (method.DeclaringType == type)
                    GenerateNativeMethodHeader(method, outputText);
            }

            outputText.AppendLine("};");
        }

        private void GenerateNativeMethodHeader(MethodInfo method, StringBuilder builder)
        {
            string translatedReturnType = TypeConverter.TranslateParameterType(method.ReturnType).NativeType;
            builder.Append(translatedReturnType + " " + method.Name + "(");
            bool notFirst = false;
            foreach (ParameterInfo parameter in method.GetParameters())
            {
                if (notFirst)
                    builder.Append(", ");
                notFirst = true;
                builder.Append(TypeConverter.TranslateParameterType(parameter.ParameterType).NativeType + " " + parameter.Name);
            }
            builder.AppendLine(");");
        }

        private void GenerateNativeAggregatedMember(Type type, StringBuilder builder)
        {
            builder.AppendLine("private:");
            builder.AppendLine(Utils.GetILBridgeTypeNameFor(type) + "* __Impl;");
        }

        protected override string GetFileName(Type type)
        {
            return Utils.GetNativeExportedClassNameFor(type) + ".h";
        }
    }

    public class NativeExporterSourceGenerator : TypeGenerator
    {
        protected override void OnGenerate(Type type, StringBuilder outputText)
        {
            outputText.AppendLine("#pragma managed");
            outputText.AppendLine("#include \"" + new ILBridgeGenerator().GetFileNameFor(type) + "\"");
            outputText.AppendLine("#pragma unmanaged");
            outputText.AppendLine("#include \"" + new NativeExporterHeaderGenerator().GetFileNameFor(type) + "\"");

            GenerateNativeConstructor(type, outputText);
            GenerateNativeDestructor(type, outputText);
            foreach (MethodInfo method in type.GetMethods())
            {
                if (method.DeclaringType == type)
                    GenerateNativeMethodImplementation(method, outputText);
            }
        }

        private void GenerateNativeMethodImplementation(MethodInfo method, StringBuilder builder)
        {
            string translatedReturnType = TypeConverter.TranslateParameterType(method.ReturnType).NativeType;
            builder.Append(translatedReturnType + " " + Utils.GetNativeExportedClassNameFor(method.DeclaringType) + "::" + method.Name + "(");
            bool notFirst = false;
            foreach (ParameterInfo parameter in method.GetParameters())
            {
                if (notFirst)
                    builder.Append(", ");
                notFirst = true;
                builder.Append(TypeConverter.TranslateParameterType(parameter.ParameterType).NativeType + " " + parameter.Name);
            }
            builder.AppendLine(") {");

            if (method.ReturnType != typeof(void))
            {
                builder.Append(TypeConverter.TranslateParameterType(method.ReturnType).NativeType + " __ReturnVal = ");
            }

            builder.Append("__Impl->" + method.Name + "(");
            notFirst = false;
            foreach (ParameterInfo parameter in method.GetParameters())
            {
                if (notFirst)
                    builder.Append(", ");
                notFirst = true;
                builder.Append(parameter.Name);
            }
            builder.AppendLine(");");

            if (method.ReturnType != typeof(void))
            {
                builder.AppendLine("return __ReturnVal;");
            }
            builder.AppendLine("}");
        }

        private void GenerateNativeDestructor(Type type, StringBuilder builder)
        {
            string className = Utils.GetNativeExportedClassNameFor(type);
            builder.AppendLine(className + "::~" + className + "() {");
            builder.AppendLine("delete __Impl;");
            builder.AppendLine("}");
        }

        //TODO: Constructor parameters if necessary
        //TODO: Several constructors on IL bridge
        private void GenerateNativeConstructor(Type type, StringBuilder builder)
        {
            string className = Utils.GetNativeExportedClassNameFor(type);
            builder.AppendLine(className + "::" + className + "() {");
            builder.AppendLine("__Impl = new " + Utils.GetILBridgeTypeNameFor(type) + ";");
            builder.AppendLine("}");
        }

        protected override string GetFileName(Type type)
        {
            return Utils.GetNativeExportedClassNameFor(type) + ".cpp";
        }
    }
}
